//
//  MyShowSelfListViewController.m
//  BosidengIphone
//
//  Created by  on 11-12-30.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "MyShowSelfListViewController.h"
#import "MyShowData.h"
#import "AppDelegate.h"
#import "SelfPageView.h"
#import "SelfShowItemsXmlParser.h"
#import "SyncRequestParseOperation.h"

@implementation MyShowSelfListViewController
@synthesize titleLabel;
@synthesize tipLabel;
@synthesize pageControl;
@synthesize txtView;
@synthesize selfShows;
@synthesize waitView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.canHanlderPageSingleTap = NO;
        self.loadAtFirst = NO;
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
    [waitView release];
    [selfShows release];
    [titleLabel release];
    [tipLabel release];
    [pageControl release];
    [txtView release];
}

#pragma mark - View lifecycle

- (void)initView {
    self.selfShows = [MyShowData getItemsByCustomerId:[AppDelegate getCustom].customId];
    //NSLog(@"request selfShows count(%d)", [selfShows count]);
    titleLabel.text = [NSString stringWithFormat:@"%@(%d)", [AppDelegate getLanguageString:@"show_my_show"], [selfShows count]];
    double count = [selfShows count];
    pageControl.numberOfPages = ceil(count / 3.0);
    pageControl.currentPage = 0;
    
    [self loadPageViews];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    pagingScrollView.backgroundColor = [UIColor clearColor];
    tipLabel.text = [AppDelegate getLanguageString:@"show_my_show_tip"];
    txtView.text=[AppDelegate getLanguageString:@"show_my_show_descript"];
    //pageControl.currentPage = 0;
    [waitView startAnimating];
    NSString *url = [NSString stringWithFormat:@"%@/GetMyShow.ashx?CustomerId=%@", [AppDelegate serviceUrl], [AppDelegate getCustom].customId];
    SelfShowItemsXmlParser *showParser = [[SelfShowItemsXmlParser alloc] init];
    SyncRequestParseOperation *operation = [[SyncRequestParseOperation alloc] initWithURLString:url xmlParser:showParser delegate:self];
    //[operation setUserInfo:[self setRequestName:key]];
    [requestQueue addOperation:operation];
    [operation release];
    [showParser release];
    //NSLog(@"request GetMyShow url = %@", url);
}

- (void)updateLocalAllShowList:(NSArray *)serviceData {
    for (int i = 0; i < [serviceData count]; i++) {
        NSDictionary *data = [serviceData objectAtIndex:i];
        NSString *uid = [data objectForKey:@"ShowID"];
        MyShowData *localData = [MyShowData getItemByID:uid];
        if (localData == nil) {
            localData = [[[MyShowData alloc] init] autorelease];
        } else {
            //判断是否需要删除本地图片
            NSString *localImages = localData.smallImage;
            NSString *serviceImages = [data objectForKey:@"SmallImage"];
            if (![localImages isEqualToString:serviceImages]) {
                [localData removeSmallImage];
            }
            
            localImages = localData.bigImage;
            serviceImages = [data objectForKey:@"BigImage"];
            if (![localImages isEqualToString:serviceImages]) {
                [localData removeBigImage];
            }
        }
        //NSLog(@"save new data(%@)", localData);
        //添加，更新最新的数据
        [localData setValue:data];
        [localData save];
    }
    //取得最新的数据集
    NSArray *localDatas = [MyShowData getItemsByCustomerId:[AppDelegate getCustom].customId];
    for (MyShowData *data in localDatas) {
        NSString *localId = data.showID;
        if ([self isNeedDeleteItem:localId serviceData:serviceData serviceKey:@"ShowID"]) {
            //NSLog(@"delete not exist data(%@)", data);
            [data removeBigImage];
            [data removeSmallImage];
        }
    }
}

- (void)syncRequestParseStatusNofity:(ParseData *)parseData {
    int status = parseData.status;
    if (status == kParseDataFailed) {
        NSLog(@"getShow Request Failed! Reason:%@", parseData.error);
    } else {
        NSArray *datas = [parseData.data objectForKey:@"items"];
        //NSLog(@"request data count(%d)", [datas count]);
        [self updateLocalAllShowList:datas];
    }
    [self initView];
    [waitView stopAnimating];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.titleLabel = nil;
    self.tipLabel = nil;
    self.pageControl = nil;
    self.txtView = nil;
    self.waitView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark Image Scroll view delegate

- (NSInteger)imageCount {
    double count = [selfShows count];
    NSInteger pageCount = ceil(count / 3.0);
    //NSLog(@"count=%.2f, pageCount=%d", count, pageCount);
    return pageCount;
}

- (UIView *)configureView:(UIView *)page atIndex:(NSInteger)index {
    if (page == nil) {
        SelfPageView *recomPage = [[[SelfPageView alloc] init] autorelease];
        recomPage.requestQueue = requestQueue;
        recomPage.recomDatas = selfShows;
        recomPage.mainViewController = mainViewController;
        page = recomPage;
    }
    [(SelfPageView *)page setPageIndex:index];
    [(SelfPageView *)page loadData];
    return page;
}

- (void)changePageIndex:(NSInteger)index {
    pageControl.currentPage = index;
}

- (void)selectPageIndex:(NSInteger)index {
    //NSLog(@"selectPageIndex=%d", index);
}

@end
